package com.wordplat.quickstart.xutils;

import com.wordplat.quickstart.xutils.common.TaskController;
import com.wordplat.quickstart.xutils.common.task.TaskControllerImpl;
import com.wordplat.quickstart.xutils.db.DbManagerImpl;
import com.wordplat.quickstart.xutils.ex.DbException;
import com.wordplat.quickstart.xutils.http.HttpManagerImpl;
import com.wordplat.quickstart.xutils.image.ImageManagerImpl;
import com.wordplat.quickstart.xutils.view.ViewInjectorImpl;

import ohos.aafwk.ability.Ability;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;

/**
 * Created by wyouflf on 15/6/10.
 * 任务控制中心, http, image, db, view注入等接口的入口.
 * 需要在在application的onCreate中初始化: x.Ext.init(this);
 *
 * @since 2021-05-09
 */
public final class x {
    private x() {
    }

    /**
     * isDebug
     *
     * @return boolean
     */
    public static boolean isDebug() {
        return Ext.debug;
    }

    /**
     * app
     *
     * @return Ability
     */
    public static Ability app() {
        return Ext.app;
    }

    /**
     * task
     *
     * @return TaskController
     */
    public static TaskController task() {
        return Ext.taskController;
    }

    /**
     * http
     *
     * @return HttpManager
     */
    public static HttpManager http() {
        if (Ext.httpManager == null) {
            HttpManagerImpl.registerInstance();
        }
        return Ext.httpManager;
    }

    /**
     * image
     *
     * @return ImageManager
     */
    public static ImageManager image() {
        if (Ext.imageManager == null) {
            ImageManagerImpl.registerInstance();
        }
        return Ext.imageManager;
    }

    /**
     * view
     *
     * @return ViewInjector
     */
    public static ViewInjector view() {
        if (Ext.viewInjector == null) {
            ViewInjectorImpl.registerInstance();
        }
        return Ext.viewInjector;
    }

    /**
     * getDb
     *
     * @param daoConfig
     * @return DbManager
     * @throws DbException
     */
    public static DbManager getDb(DbManager.DaoConfig daoConfig) throws DbException {
        return DbManagerImpl.getInstance(daoConfig);
    }

    /**
     * Ext
     *
     * @since 2021-05-09
     */
    public static class Ext {
        private static boolean debug;
        private static Ability app;
        private static TaskController taskController;
        private static HttpManager httpManager;
        private static ImageManager imageManager;
        private static ViewInjector viewInjector;

        private Ext() {
        }

        /**
         * init
         *
         * @param ability
         */
        public static void init(Ability ability) {
            TaskControllerImpl.registerInstance();
            if (Ext.app == null) {
                Ext.app = ability;
            }
        }

        public static void setDebug(boolean debug) {
            Ext.debug = debug;
        }

        /**
         * setTaskController
         *
         * @param taskController
         */
        public static void setTaskController(TaskController taskController) {
            if (Ext.taskController == null) {
                Ext.taskController = taskController;
            }
        }

        public static void setHttpManager(HttpManager httpManager) {
            Ext.httpManager = httpManager;
        }

        public static void setImageManager(ImageManager imageManager) {
            Ext.imageManager = imageManager;
        }

        public static void setViewInjector(ViewInjector viewInjector) {
            Ext.viewInjector = viewInjector;
        }
    }
}
